home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / RDSERVKY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-31  |  3.7 KB  |  123 lines

  1. /*
  2.  * $Source: /afs/athena.mit.edu/astaff/project/kerberos/src/lib/krb/RCS/read_service_key.c,v $
  3.  * $Author: jon $
  4.  *
  5.  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
  6.  * of Technology.
  7.  *
  8.  * For copying and distribution information, please see the file
  9.  * <mit-copyright.h>.
  10.  */
  11.  
  12. #ifndef lint
  13. static char *rcsid_read_service_key_c =
  14. "$Id: read_service_key.c,v 4.10 90/03/10 19:06:56 jon Exp $";
  15. #endif /* lint */
  16.  
  17. #include <mit_copy.h>
  18. #include <krb.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. /*
  23.  * The private keys for servers on a given host are stored in a
  24.  * "srvtab" file (typically "/etc/srvtab").  This routine extracts
  25.  * a given server's key from the file.
  26.  *
  27.  * read_service_key() takes the server's name ("service"), "instance",
  28.  * and "realm" and a key version number "kvno", and looks in the given
  29.  * "file" for the corresponding entry, and if found, returns the entry's
  30.  * key field in "key".
  31.  * 
  32.  * If "instance" contains the string "*", then it will match
  33.  * any instance, and the chosen instance will be copied to that
  34.  * string.  For this reason it is important that the there is enough
  35.  * space beyond the "*" to receive the entry.
  36.  *
  37.  * If "kvno" is 0, it is treated as a wild card and the first
  38.  * matching entry regardless of the "vno" field is returned.
  39.  *
  40.  * This routine returns KSUCCESS on success, otherwise KFAILURE.
  41.  *
  42.  * The format of each "srvtab" entry is as follows:
  43.  *
  44.  * Size            Variable        Field in file
  45.  * ----            --------        -------------
  46.  * string        serv            server name
  47.  * string        inst            server instance
  48.  * string        realm            server realm
  49.  * 1 byte        vno            server key version #
  50.  * 8 bytes        key            server's key
  51.  * ...            ...            ...
  52.  */
  53.  
  54.  
  55. /*ARGSUSED */
  56. read_service_key(service,instance,realm,kvno,file,key)
  57.     char *service;              /* Service Name */
  58.     char *instance;             /* Instance name or "*" */
  59.     char *realm;                /* Realm */
  60.     int kvno;                   /* Key version number */
  61.     char *file;                 /* Filename */
  62.     char *key;                  /* Pointer to key to be filled in */
  63. {
  64.     char serv[SNAME_SZ];
  65.     char inst[INST_SZ];
  66.     char rlm[REALM_SZ];
  67.     unsigned char vno;          /* Key version number */
  68.     int wcard;
  69.  
  70.     int stab, open();
  71.  
  72.     if ((stab = open(file, 0, 0)) < NULL)
  73.         return(KFAILURE);
  74.  
  75.     wcard = (instance[0] == '*') && (instance[1] == '\0');
  76.  
  77.     while(getst(stab,serv,SNAME_SZ) > 0) { /* Read sname */
  78.         (void) getst(stab,inst,INST_SZ); /* Instance */
  79.         (void) getst(stab,rlm,REALM_SZ); /* Realm */
  80.         /* Vers number */
  81.         if (read(stab,(char *)&vno,1) != 1) {
  82.         close(stab);
  83.             return(KFAILURE);
  84.     }
  85.         /* Key */
  86.         if (read(stab,key,8) != 8) {
  87.         close(stab);
  88.             return(KFAILURE);
  89.     }
  90.         /* Is this the right service */
  91.         if (strcmp(serv,service))
  92.             continue;
  93.         /* How about instance */
  94.         if (!wcard && strcmp(inst,instance))
  95.             continue;
  96.         if (wcard)
  97.             (void) strncpy(instance,inst,INST_SZ);
  98.         /* Is this the right realm */
  99. #ifdef ATHENA_COMPAT
  100.     /* XXX For backward compatibility:  if keyfile says "Athena"
  101.        and caller wants "ATHENA.MIT.EDU", call it a match */
  102.         if (strcmp(rlm,realm) &&
  103.         (strcmp(rlm,"Athena") ||
  104.          strcmp(realm,"ATHENA.MIT.EDU")))
  105.         continue;
  106. #else /* ! ATHENA_COMPAT */
  107.         if (strcmp(rlm,realm)) 
  108.         continue;
  109. #endif /* ATHENA_COMPAT */
  110.  
  111.         /* How about the key version number */
  112.         if (kvno && kvno != (int) vno)
  113.             continue;
  114.  
  115.         (void) close(stab);
  116.         return(KSUCCESS);
  117.     }
  118.  
  119.     /* Can't find the requested service */
  120.     (void) close(stab);
  121.     return(KFAILURE);
  122. }
  123.